package leetcode_1_200;

import node.ListNode;

import java.util.HashSet;
import java.util.Set;

public class LeeCode_160 {
    public static void main(String[] args) {
        ListNode aft = new ListNode(4, new ListNode(5, new ListNode(4)));
        ListNode ha = new ListNode(2, new ListNode(2, aft));
        ListNode hb = new ListNode(2, new ListNode(2, aft));
        System.out.println(getIntersectionNode(ha, hb));
    }
    private static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> sa = new HashSet<>();
        while (headA != null){
            sa.add(headA);
            headA = headA.next;
        }
        while (headB != null){
            if (sa.contains(headB))
                return headB;
            headB = headB.next;
        }
        return null;
    }
}
